package com.liehu.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.liehu.business.pojo.LeaveMessage;
import com.liehu.business.service.LeavemessageService;
import com.liehu.business.service.ReplyService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Controller控制器
 */
@RestController
@RequestMapping("/leavemessage")
@CrossOrigin //跨域处理
@Api(value = "/leavemessage",description = "留言")
public class LeaveMessageController {
    @Autowired
    private ReplyService replyService;
    @Autowired
    private LeavemessageService leavemessageService;

    /**
     * 查询所有
     */
    @ApiOperation(value = "查询全部回复", notes = "查询全部回复")//描述
    @GetMapping()
    public Result findAll(){
        return  new Result(true, StatusCode.OK,"查询成功！",leavemessageService.findAll());
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")//描述
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id){
        return  new Result(true, StatusCode.OK,"查询成功！",leavemessageService.findById(id));
    }
    /**
     * 插入一条数据
     */
    @ApiOperation(value = "插入一条数据", notes = "插入一条数据，仅限客户操作")//描述
    @PostMapping()
    public Result save(@RequestBody LeaveMessage leaveMessage){
        leavemessageService.save(leaveMessage);
        return  new Result(true, StatusCode.OK,"新增成功！");
    }

    /**
     * 根据id修改记录
     * @param leaveMessage
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id修改记录", notes = "根据id修改记录，仅限客户操作")//描述
    @PutMapping("/{id}")
    public Result update(@RequestBody LeaveMessage leaveMessage,@PathVariable("id") String id){
        leaveMessage.setId(id);//给reply植入id
        leavemessageService.update(leaveMessage);
        return  new Result(true, StatusCode.OK,"修改成功！");
    }
    /**
     * 根据id删除
     */
    @ApiOperation(value = "根据id删除", notes = "根据id删除-仅限管理员操作")//描述
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id){
        leavemessageService.deleteById(id);
        return  new Result(true, StatusCode.OK,"删除成功！");
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "不带条件的分页查询")//描述
    @GetMapping("/{page}/{size}")
    public Result findByPage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<LeaveMessage> list = leavemessageService.findByPage(page, size);
        return  new Result(true, StatusCode.OK,"查询成功！",new PageResult<LeaveMessage>(list.getTotal(),list.getRecords()));
    }
    /**
     * 带条件的分页查询
     */
    @ApiOperation(value = "分页查询", notes = "带条件的分页查询")//描述
    @PostMapping("/search/{page}/{size}")
    public Result findByPageSearch(@RequestBody LeaveMessage leaveMessage,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        Page<LeaveMessage> list = leavemessageService.findByPage(leaveMessage, page, size);
        return  new Result(true, StatusCode.OK,"查询成功！",new PageResult<LeaveMessage>(list.getTotal(),list.getRecords()));
    }

    /**
     * 审核留言-仅限管理员操作-leaveMessageId为留言id,
     * status为留言状态，0为待审核，1为审核通过
     * @param leaveMessageId
     * @param status
     * @return
     */
    @ApiOperation(value = "审核留言", notes = "审核留言-仅限管理员操作-leaveMessageId为留言id,status为留言状态，0为待审核，1为审核通过")//描述
    @PutMapping("/update/status/{leaveMessageId}/{status}")
    public Result updateStatus(@PathVariable("leaveMessageId") String leaveMessageId,@PathVariable("status")String status){
        leavemessageService.updateStatus(leaveMessageId,status);
        return  new Result(true, StatusCode.OK,"审核通过！");
    }




}
